// RtspCamera.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Controls.Material 2.15
import QtGraphicalEffects 1.15
import QtMultimedia 5.15

Item {
    id: cameraControl
    property var robotId: ""
    property string cameraUrl: "rtsp://192.168.1.10:554/user=admin&password=&channel=1&stream=0.sdp?"
    readonly property int ptzTiltUp: 21
    readonly property int ptzTiltDown: 22
    readonly property int ptzPanLeft: 23
    readonly property int ptzPanRight: 24

    function initCamera() {
        console.log("Initializing camera...")
        if (videoContainer) {
            videoContainer.forceActiveFocus()
            console.log("Video container focused")
        }
    }


    MediaPlayer {
       id: mediaPlayer
      source: cameraUrl
        onStatusChanged: {
        console.log("cameraUrl: " + cameraUrl)
        console.log("Media Player Status: " + mediaPlayer.status)
        console.log("Error: " + mediaPlayer.error + " - " + mediaPlayer.errorString)
        console.log("Has video: " + mediaPlayer.hasVideo)
        console.log("Playback state: " + mediaPlayer.playbackState)
        }
        
        onError: {
            console.log("Media Player Error - Code: " + mediaPlayer.error + ", String: " + mediaPlayer.errorString)
        }
   }

    // 视频显示区域
    Rectangle {
        id: videoContainer
        anchors.fill: parent
        color: "black"
        radius: 8
        layer.enabled: true
        layer.effect: DropShadow {
            radius: 8
            samples: 16
            color: "#80000000"
        }

        VideoOutput {
            anchors.fill: parent
            source: mediaPlayer
        }

        // 添加无视频流提示
        Text {
            anchors.centerIn: parent
            text: "                        无摄像头视频流!\n请检查摄像头是否正常启动或RTSP地址配置!"
            color: "white"
            font.pixelSize: 24
            visible: mediaPlayer.status === MediaPlayer.Invalid || 
                    mediaPlayer.status === MediaPlayer.Stalled ||
                    (mediaPlayer.status === MediaPlayer.Buffered && !mediaPlayer.hasVideo)
            z: 1  // 确保在视频上方显示

        }

        MouseArea {
            anchors.fill: parent
            onClicked: {
                mediaPlayer.play()
            }
        }
    }
}